package {
	
	import com.mac.aircraft.Aircraft;
	import com.mac.flightpath.Flightpath;
	import com.mac.components.HUD;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	// set this projects meta data here
	[SWF(width="600", height="450", frameRate="24", backgroundColor="0xFFFFFF")]
	
	public class mac extends Sprite	{
		
		public var score:int = 0;
		public static var WIDTH:int		= 600;
		public static var HEIGHT:int	= 450;
		
		private var hud:HUD;
		
		private var flightpath:Flightpath;
		
		public function mac() {
						
			// load the background image and place it on the stage	
			var req:URLRequest = new URLRequest( "../assests/bg.jpg" );
			var ldr:Loader = new Loader();
			ldr.load( req );
	
			hud = new HUD( WIDTH, HEIGHT / 9 )

			addChild( ldr );
			addChild( hud );
			
			// start the game loop
			addEventListener( Event.ENTER_FRAME , onGameLoop );
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}

		// should be called 24 times a second		
		public function onGameLoop( evt:Event ):* {
			
			hud.log( 'onGameLoop ' + new Date().toTimeString() );
			
			// create any new aircraft
			if( generateNewAircraft() ) {
				addChild( new Aircraft( WIDTH, HEIGHT ) );
			}

			// aircraft update themselves with their own 'enter_stage' event handlers
			
			// check for any collisions/successful landings/other events
			var coordArr:Array = new Array();
			for ( var i:int = 0; i < numChildren; i++ ) {
				
				// get only aircraft objects from the added children
				var ac:DisplayObject = getChildAt(i);
				if ( !(ac is Aircraft) ) continue;
				 
				// get x/y coords to check for collision
				var coord:Point = new Point(ac.x, ac.y);
				coordArr.push( coord );

				// check for other events (godzilla, ufo sighting, fleet of carriers, etc)
			}
		}
		
		// check to see if we should create an aircraft
		public function generateNewAircraft():Boolean {

			var create:Boolean = false;
			var ceiling:Number = 120;
			var rand:Number = Math.random() * ceiling;
			
			if ( rand < score + 1 ) {
				create = true;
			}
			
			return create;
		}

		
		private function mouseDownHandler(event:MouseEvent):void {
			graphics.clear();
			graphics.lineStyle(0.5,0x000000,0.25);
			
			flightpath = new Flightpath(); 
		}
		
		private function mouseUpHandler(event:MouseEvent):void {
			graphics.lineStyle();
			graphics.clear();
			flightpath.draw(graphics);
			hud.log( 'Points: ' + flightpath.filteredPoints.length + ', Beziers: ' + flightpath.beziers.length );
			flightpath.reset();
		}
		
		private function mouseMoveHandler(event:MouseEvent):void {
			if(event.buttonDown) {
				var point:Point = new Point(event.stageX, event.stageY);
			
				if(flightpath.rawPoints.length >0) {
					var previousPoint:Point = flightpath.rawPoints[flightpath.rawPoints.length-1];
					graphics.lineTo(point.x,point.y);
				} else {
					graphics.moveTo(point.x,point.y);
				}

				flightpath.addPoint(point);
				
			} 
		}
		
	}
}
